//
//  ApplyAPI.swift
//  kano
//
//  Created by Frank on 2024/9/6.
//

import Foundation
import Moya
import KakaJSON

enum ApplyAPI {
    
    case applyAuto(_ parameters: Dictionary<String, Any>? = nil)
    case submit(_ parameters: Dictionary<String, Any>? = nil)
    case cache(_ parameters: Dictionary<String, Any>? = nil)
    case submitStatus(_ parameters: Dictionary<String, Any>? = nil)
    case modifyIdNum(_ parameters: Dictionary<String, Any>? = nil)

    case pikerSources(_ parameters: Dictionary<String, Any>? = nil)
    case totalSubmit(_ parameters: Dictionary<String, Any>? = nil)

    case license(_ parameters: Dictionary<String, Any>? = nil)
    case livenessResult(_ parameters: Dictionary<String, Any>? = nil)
    case checkOldMobile(_ parameters: Dictionary<String, Any>? = nil)
}

extension ApplyAPI : TargetType {
    var baseURL: URL {
//        switch self {
//        case .submitStatus:
//            return URL(string: kConfigUrl)!
//        default:
//            return URL(string: NetConfig.readBaseUrl())!
//        }
        return URL(string: NetConfig.readBaseUrl())!
    }
    
    var path: String {
        switch self {
            //Post
        case .applyAuto:
            let path = NetConfig.fullNormal(path: "/riskManagementAuth")
            return path
            
        case .submit:
            let path = NetConfig.fullApply(path: "/submitTaskV1")
            return path
            
        case .cache:
            let path = NetConfig.fullNormal(path: "/setCreditResult")
            return path
            
        case .modifyIdNum:
            let path = NetConfig.fullNormal(path: "/getOldPhoneInfo")
            return path
        case .totalSubmit:
            let path = NetConfig.fullNormal(path: "/submitCreditApp")
            return path
            
            //Get
        case .pikerSources:
//            let path = NetConfig.fullApply(path: "/submitTask")
            let path = NetConfig.fullApply(path: "/config_info")
            return path
        case .submitStatus:
//            let path = NetConfig.fullApply(path: "/taskStatusV1")
            let path = NetConfig.fullApply(path: "/t_status")
            return path
            
        case .license:
            let path = NetConfig.fullApply(path: "/livenessvalidationresult")
            return path
            
        case .livenessResult:
            let path = NetConfig.fullApply(path: "/environment_sustainability")
            return path
            
        case .checkOldMobile:
            let path = NetConfig.fullNormal(path: "/getOldPhoneInfo")
            return path
            
        default:
            return ""
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .pikerSources: fallthrough
        case .license: fallthrough
        case .submitStatus:
            return .get
        default:
            return .post
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .submit(let parameters): fallthrough
        case .cache(let parameters): fallthrough
        case .modifyIdNum(let parameters): fallthrough
        case .totalSubmit(let parameters): fallthrough
        case .livenessResult(let parameters): fallthrough
        case .checkOldMobile(let parameters): fallthrough
        case .applyAuto(let parameters):
            if kisEncrypt == 1 {
                let data: Dictionary<String, Any>
                if (parameters == nil) {
                    data = ["body" : ""]
                } else {
                    let bodyData = KAEncrypt.ka_encrypt(parameters!.jsonString()!)
                    data = ["body" : bodyData]
                }
                return .requestCompositeParameters(bodyParameters: data as [String : Any], bodyEncoding: JSONEncoding.default, urlParameters: NetConfig.urlParam())
            } else {
                return .requestCompositeParameters(bodyParameters:parameters ?? ["" : ""], bodyEncoding: JSONEncoding.default, urlParameters: NetConfig.urlParam())
            }
        case .pikerSources(let parameters): fallthrough
        case .license(let parameters): fallthrough
        case .submitStatus(let parameters):
            return .requestParameters(parameters: parameters!, encoding: URLEncoding.default)
        default:
            return .requestPlain
        }
    }
    
    var headers: [String : String]? {
        return ["Content-Type":"application/json; charset=utf-8",
                "SW-K-ID": UIDevice.ka_UUID.toBase64()]
    }
}


